package com.prj.ufdm.demo.config;

import java.util.concurrent.Executor;

import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

/**
 * 多线程执行配置（通过 @Async 注解来声明一个异步任务）
 * @author Administrator
 */
@Configuration
@EnableAsync
public class AsyncConfig implements AsyncConfigurer {
	
	@Value("${check.thread.size:10}")
	private int corePoolSize;
	
	@Override
	public Executor getAsyncExecutor() {
		
		ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
		//线程池维护线程的最少数量  
		taskExecutor.setCorePoolSize(corePoolSize);
		//线程池所使用的缓冲队列
		taskExecutor.setQueueCapacity(150);
		//线程池维护线程的最大数量  
		taskExecutor.setMaxPoolSize(1000);
		//线程池维护线程所允许的空闲时间
		taskExecutor.setKeepAliveSeconds(30000);
		taskExecutor.initialize();
		return taskExecutor;
	}

    @Bean
    public Executor taskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(corePoolSize);
        executor.setMaxPoolSize(1000);
        executor.setQueueCapacity(150);
        executor.initialize();
        return executor;
    }
	
	@Override
	public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
		return null;
	}

}
